/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.manager.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.tools.config.Configuration;
import org.springframework.stereotype.Component;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.modules.autocode.dto.Column;
import cn.com.ksplatform.modules.autocode.dto.ColumnArray;
import cn.com.ksplatform.modules.autocode.dto.Events;
import cn.com.ksplatform.modules.autocode.dto.Propertys;
import cn.com.ksplatform.modules.autocode.global.Config;
import cn.com.ksplatform.modules.autocode.manager.model.Module;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;
import cn.com.ksplatform.modules.autocode.util.XmlUtil;

/**
 * ClassName:SenceService.java <br/>
 * Date: 2015年11月5日 <br/>
 * 场景业务类
 * @author Mr.Xiao
 * @version 1.0
 */
@Component("modules.autocode.SenceService")
public class SenceService extends BaseService{
	/**
	 * 保存场景
	 * @param sence
	 * @throws Exception 
	 */
	public void saveSence(Sence sence) throws Exception {
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+"sence.xml";
		File f = new File(path);
		if(f.exists()){
			throw new Exception("场景已存在");
		}else{
			f.getParentFile().mkdir();
			createVersion(sence);
		}
		FileOutputStream ous = new FileOutputStream(f);
		sence.setModule(null);
		XmlUtil.saveObject2Xml(sence, ous);
	}
	/**
	 * 更新场景
	 * @param sence
	 * @throws Exception 
	 */
	public void updateSence(Sence sence) throws Exception {
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+"sence.xml";
		File f = new File(path);
		if(f.exists()){
			FileOutputStream ous = new FileOutputStream(f);
			sence.setModule(null);
			XmlUtil.saveObject2Xml(sence, ous);
		}else{
			throw new Exception("场景不存在");
		}
	}
	/**
	 * 创建版本
	 * @param sence
	 * @throws Exception
	 */
	public void createVersion(Sence sence) throws Exception{
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			throw new Exception("版本已存在");
		}else{
			f.mkdir();
		}
	}
	/**
	 * 获取场景代码
	 * @param module
	 * @return
	 * @throws FileNotFoundException 
	 */
	public List<Sence> getSencesByModule(Module module) throws FileNotFoundException{
		List<Sence> sences = new ArrayList<Sence>();
		String path = module.getProject().getBulidPath()+File.separator+module.getModulesCode();
		File senceFolder = new File(path);
		File[] fs = senceFolder.listFiles();
		for (File f : fs) {
			if(f.isDirectory()){
				File senceConfig = new File(f.getPath()+File.separator+"sence.xml");
				if(f.exists()){
					Sence sence = (Sence) XmlUtil.getObjectFromXml(Sence.class, new FileInputStream(senceConfig));
					sences.add(sence);
				}else{
					Log.debug("忽略目录:"+f.getPath());
				}
			}
		}
		return sences;
	}
	/**
	 * 保存列
	 * @param sence
	 * @param columns
	 * @throws Exception
	 */
	public void addCloumnsToSence(Sence sence, ColumnArray columnarray) throws Exception {
		addCloumnsToSence(sence,columnarray,"Columns.xml");
	}
	/**
	 * 保存列 自定义文件名称
	 * @param sence
	 * @param columnarray
	 * @param fileName
	 * @throws Exception
	 */
	public void addCloumnsToSence(Sence sence, ColumnArray columnarray,String fileName) throws Exception {
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			XmlUtil.saveObject2Xml(columnarray,new FileOutputStream(f.getPath()+File.separator+fileName));
			updateSence(sence);
		}else{
			throw new Exception("版本不存在");
		}
	}
	/**
	 * 获取列
	 * @param sence
	 * @return
	 * @throws Exception
	 */
	public List<Column> getCloumnsBySence(Sence sence) throws Exception {
		return getCloumnsBySence(sence,"Columns.xml");
	}
	/**
	 * 获取列 自定义文件名
	 * @param sence
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public List<Column> getCloumnsBySence(Sence sence,String fileName) throws Exception {
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			ColumnArray columnarray = (ColumnArray) XmlUtil.getObjectFromXml(ColumnArray.class, new FileInputStream(f.getPath()+File.separator+fileName));
			return columnarray.getColumns();
		}else{
			throw new Exception("版本不存在");
		}
	}
	
	public Object[] getChildrenCloumnsBySence(Sence sence,String fileName) throws Exception {
		String path;
		if(sence.getModule().getProject() == null){
			path = Config.PATH+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		}else{
			path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		}
		Object[] obj = new Object[2];
		List<ColumnArray> list = new ArrayList<ColumnArray>();
		List<String> names = new ArrayList<String>();
		File f = new File(path);
		if(f.exists() && f.isDirectory()){
			File[] childrens = f.listFiles();
			for(File c : childrens){
				if(c.isFile() && c.getName().startsWith(fileName)){
					String fullName = c.getName();
					
					names.add(fullName.substring(9).replace(".xml",""));
					ColumnArray columnarray = (ColumnArray) XmlUtil.getObjectFromXml(ColumnArray.class, new FileInputStream(f.getPath()+File.separator+fullName));
					list.add(columnarray);
				}
			}
			obj[0] = list;
			obj[1] = names;
			return obj;
		}else{
			throw new Exception("版本不存在");
		}
	}
	
	/**
	 * 保存属性 自定义名称
	 * @param sence
	 * @param propertys
	 * @param fileName
	 * @throws Exception
	 */
	public void addPropertysToSence(Sence sence, Propertys propertys,String fileName) throws Exception {
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			XmlUtil.saveObject2Xml(propertys,new FileOutputStream(f.getPath()+File.separator+fileName));
		}else{
			throw new Exception("版本不存在");
		}
	}
	
	public void addPropertysToSence(Sence sence, Propertys propertys) throws Exception{
		addPropertysToSence(sence,propertys,"Propertys.xml");
	}
	
	/**
	 * 保存事件 自定义名称
	 * @param sence
	 * @param events
	 * @param fileName
	 * @throws Exception
	 */
	public void addEventsToSence(Sence sence, Events events,String fileName) throws Exception {
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			XmlUtil.saveObject2Xml(events,new FileOutputStream(f.getPath()+File.separator+fileName));
		}else{
			throw new Exception("版本不存在");
		}
	}
	
	public void addEventsToSence(Sence sence, Events events) throws Exception {
		addEventsToSence(sence,events,"Events.xml");
	}
	
	/**
	 * 获取属性
	 * @param sence
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public Propertys getPropertysBySence(Sence sence,String fileName)throws Exception{
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			Propertys propertys = (Propertys) XmlUtil.getObjectFromXml(Propertys.class, new FileInputStream(f.getPath()+File.separator+fileName));
			return propertys;
		}else{
			throw new Exception("版本不存在");
		}
	}
	
	public Propertys getPropertysBySence(Sence sence) throws Exception{
		return getPropertysBySence(sence,"Propertys.xml");
	}
	
	/**
	 * 获取事件
	 * @param sence
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public Events getEventsBySence(Sence sence,String fileName)throws Exception{
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			Events events = (Events) XmlUtil.getObjectFromXml(Events.class, new FileInputStream(f.getPath()+File.separator+fileName));
			return events;
		}else{
			throw new Exception("版本不存在");
		}
	}
	
	public Events getEventsBySence(Sence sence) throws Exception{
		return getEventsBySence(sence,"Events.xml");
	}
	
	/**
	 * 删除子表
	 * @param sence
	 * @param childrenId
	 * @return
	 * @throws Exception 
	 */
	public void removeCloumnsToSence1_ChildrenTable(Sence sence, String childrenId) throws Exception {
		String path = sence.getModule().getProject().getBulidPath()+File.separator+sence.getModule().getModulesCode()+File.separator+sence.getSenceCode()+File.separator+sence.getSenceVersion()+File.separator+childrenId;
		File f = new File(path);
		if(f.exists() && f.isFile()){
			f.delete();
		}else{
			throw new Exception("版本不存在");
		}
	}
}
